export interface routerTagsType {
  pathName: string;
  label: string;
}
interface actionType {
  type: string;
  data: routerTagsType;
}

const routerTags: routerTagsType[] = [];

// 处理函数
function preStateFind(preTagState: routerTagsType[], data: routerTagsType, sign: string) {
  const index = preTagState.findIndex(item => item.pathName == data.pathName);
  if (sign == 'add') {
    if (index == -1) {
      preTagState.push(data);
    } else {
      preTagState.splice(index, 1);
      preTagState.push(data);
    }
  } else {
    preTagState.splice(index, 1);
  }

  return preTagState;
}

const setRouterTags = (preTagState: routerTagsType[] = routerTags, action: actionType) => {
  const { type, data } = action;
  // 多个reducer是共享这一空间（switch上面的这部分）？？？

  switch (type) {
    case 'ADD_ROUTER_TAG':
      preTagState = preStateFind(preTagState, data, 'add');
      return [...preTagState];
    case 'DEL_ROUTER_TAG':
      preTagState = preStateFind(preTagState, data, 'del');
      return [...preTagState];
    default:
      return preTagState;
  }
};

export default setRouterTags;
